import mysql.connector  
  
# 创建连接  
cnx = mysql.connector.connect(user='root', password='1778135652ming',  
                              host='localhost',  
                              database='test_db')  
  
# 创建一个游标对象 cursor  
cursor = cnx.cursor()
# 执行 SQL 查询  
query = ("SELECT * FROM table_name")  
cursor.execute(query)  
  
# 获取所有记录列表  
rows = cursor.fetchall()  
  
for row in rows:  
    print(row)
# 插入一行数据  
add_row = ("INSERT INTO table_name (column1, column2) VALUES (%s, %s)",  
           ('value1', 'value2'))  
cursor.execute(*add_row)  
  
# 提交事务  
cnx.commit()
# 关闭游标和连接  
cursor.close()  
cnx.close()